package com.fms.fmsapi.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fms.fmsapi.entity.R;
import com.fms.fmsapi.entity.Role;
import com.fms.fmsapi.entity.User;
import com.fms.fmsapi.service.UserService;
import com.fms.fmsapi.service.impl.RoleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张力文
 * @since 2024-04-03
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleServiceImpl roleService;

    @Autowired
    UserService userService;
    @PutMapping
    public R add(@RequestBody Role role){
        boolean save = roleService.save(role);
        if(save){
            return R.success("新增成功");
        }else {
            return R.success("新增失败");
        }
    }

    @GetMapping("/list")
    public R list(String keyword){
        List<Role> list = roleService.list(new LambdaQueryWrapper<Role>().like(Role::getName, keyword));
        return R.success(list);
    }

    @GetMapping("/delete")
    public R delete(String uuid){
        System.out.println(uuid);
        boolean res = roleService.removeById(uuid);
        if(res){
            return R.success("删除成功");
        }else {
            return R.success("删除失败");
        }
    }

    @PostMapping
    public R edit(@RequestBody Role role){
        boolean res = roleService.updateById(role);
        if(res){
            return R.success("编辑成功");
        }else {
            return R.success("编辑失败");
        }
    }

    @PutMapping("/empower")
    public R empowerRole(String userUuid,String roleUuid){
        User user = userService.getById(userUuid);
        user.setRoleId(roleUuid);
        if (userService.updateById(user)){
            return R.success("授权成功");
        }else {
            return R.success("授权失败");
        }
    }

}

